const state = {

  userLocationInfo: null, //定位信息
  curProvince: null, //当前定位省
  curCity: null, //当前定位市
  curDistrict: null, //当前定位区
  curTown: null, //当前定位街道
  selectCity: uni.getStorageSync("userSelectCity") || [], //选中的城市列表
  selectLocationInfo: null, //选中的区域信息或省份/市/区的定位信息描述，用于检索定位信息的
  curProvinceIndex: 0, //当前选中的省份索引值，从0开始计数，应该检索数
  municipality: [{
    id: "110000",
    name: "北京",
    fullname: "北京市",
    pinyin: ["bei", "jing"],
    location: {
      lat: 39.905023,
      lng: 116.724502
    },
    cidx: [0, 15]
  }, {
    id: "120000",
    name: "天津",
    fullname: "天津市",
    pinyin: ["tian", "jin"],
    location: {
      lat: 39.085318,
      lng: 117.201509
    },
    cidx: [16, 31]
  }, {
    id: "310000",
    name: "上海",
    fullname: "上海市",
    pinyin: ["shang", "hai"],
    location: {
      lat: 31.230525,
      lng: 121.473667
    },
    cidx: [102, 117]
  }, {
    id: "500000",
    name: "重庆",
    fullname: "重庆市",
    pinyin: ["chong", "qing"],
    location: {
      lat: 29.563707,
      lng: 106.550483
    },
    cidx: [297, 334]
  }], //直辖市
}

const mutations = {

  setUserLocationInfo(state, data) {
    state.userLocationInfo = data
    state.curProvince = {
      text: data.ad_info.province,
      code: data.ad_info.city_code.slice(3)
    };
    state.curProvince = {
      text: data.ad_info.province,
      code: data.ad_info.adcode.slice(0, 2) + "0000"
    };
    state.curCity = {
      text: data.ad_info.city,
      code: data.ad_info.city_code
    };
    state.curDistrict = {
      text: data.ad_info.district,
      code: data.ad_info.adcode
    };
    state.curTown = {
      text: data.address_reference.town.title,
      code: data.address_reference.town.id
    };
    // if (!uni.getStorageSync("userSelectCity")) {
    //   state.selectCity = [{
    //     fullname: data.ad_info.province,
    //     name: data.ad_info.province,
    //     id: data.ad_info.city_code.slice(3)
    //   }]
    //   uni.setStorageSync("userSelectCity", [{
    //     fullname: data.ad_info.province,
    //     name: data.ad_info.province,
    //     id: data.ad_info.adcode.slice(0, 2) + "0000"
    //   }])
    // }
  },
  setSelectCity(state, data) {
    console.log("78787878", data);
    state.selectCity = data
  },
  setcurProvinceIndex(state, data) {
    state.curProvinceIndex = data
  },
  setcurProvince(state, data) {
    console.log("101", data);
    state.curProvince = {
      text: data.name,
      code: data.id
    }
  }
}

const actions = {
  setLocationInfo({
    commit
  }, data) {
    commit('setUserLocationInfo', data)
  }
}

const getters = {
  getUserLocationInfo: state => state.userLocationInfo,
  getCurProvince: state => state.curProvince,
  getCurCity: state => state.curCity,
  getCurDistrict: state => state.curDistrict,
  getCurTown: state => state.curTown,
  getSelectCity: state => state.selectCity,
  getMunicipality: state => state.municipality,
  getcurProvinceIndex: state => state.curProvinceIndex,
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}